<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\User;
use App\Http\Requests\AddUserVerify;
use App\Http\Requests\SetStatusVerify;
use App\Post;
use Illuminate\Support\Facades\Auth;

class User_mangement extends Controller
{
    //获取所有用户
    public function getAllUsers()
    {
        $res = User::select('id', 'user_name', 'account', 'status', 'rating')
            ->orderBy('rating', 'DESC')
            ->paginate(7);
        return Response()->success(200, '成功', $res);
    }

    //新增用户
    public function postAddUser(AddUserVerify $request)
    {
        $id_now = null;
        $req = $request->all();
        $id_now = Auth::id();
        if ($id_now == null) {
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')->where('id', $id_now)->get()->toArray();
        if ($stu == null) {
            return response()->fail(100, '当前登录账户已被删除或不存在', null);
        }
        if ($stu[0]['status'] < 0) {
            $serch = User::where('account', '=', $request['account'])
                ->first();
            if ($serch != null) {
                return response()->fail(100, '账户已存在', null);
            }
            if ($req != null) {
                $data = new User();
                $data->user_name = $req['user_name'];
                $data->account = $req['account'];
                $data->status = $req['status'];
                $res = $data->save();
            }
            if ($res) {
                return response()->success(200, '新增成功', null);
            } else {
                return response()->fail(100, '新增失败,请重试', null);
            }
        } else {
            return response()->fail(100, '权限不够', null);
        }
    }

    //重置密码
    public function getResetPass($id)
    {

        $res = User::find($id);
        if ($res != null) {
            $res->password = encrypt(666666);
            $bool = $res->save();
            if ($bool != false) {
                return response()->success(200, '重置密码成功', null);
            } else {
                return response()->fail(100, '重置密码失败', null);
            }
        } else {
            return response()->fail(100, '用户不存在', null);
        }
    }

    //设置用户状态
    public function postSetStatus(SetStatusVerify $request, $id)
    {
        $id_now = null;
        $id_now = Auth::id();
        if ($id_now == null) {
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')
            ->where('id', $id_now)
            ->get()
            ->toArray();
        $res = User::find($id);
        if ($stu[0]['status'] < 0) {
            if ($res != null) {
                if ($id == $id_now) {
                    return Response()->fail(100, '不能设置自己的状态', null);
                } else {
                    $res->status = $request->status;
                    $bool = $res->save();
                    if ($bool) {
                        return Response()->success(200, '修改成功', null);
                    } else {
                        return Response()->fail(100, '修改失败,请重试', null);
                    }
                }
            } else {
                return Response()->fail(100, '用户不存在', null);
            }
        } else {
            return Response()->fail(100, '权限不够!', null);
        }
    }

    //软删除用户
    public function getSoftDelete($id)
    {
        $id_now = Auth::id();
        // dd()
        if ($id_now == null) {
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')->where('id', $id_now)->get()->toArray();
        $res = User::find($id);
        if ($stu[0]['status'] < 0) {
            if ($res != null) {
                if ($id_now == $id) {
                    return Response()->fail(100, '不能删除自己', null);
                } else {
                    $res->delete();
                    return Response()->success(200, '删除成功', null);
                }
            } else {
                return Response()->fail(100, '用户不存在或已被删除', null);
            }
        } else {
            return Response()->fail(100, '权限不够', null);
        }
    }

    //恢复用户
    public function getRecovery($id)
    {
        $id_now = null;
        $id_now = Auth::id();
        if ($id_now == null) {
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')->where('id', $id_now)->get()->toArray();
        $res = User::onlyTrashed()->find($id);
        if ($stu[0]['status'] < 0) {
            if ($res != null) {
                $bool = $res->restore();
                if ($bool != false) {
                    return Response()->success(200, '恢复成功', null);
                } else {
                    return Response()->fail(100, '恢复失败', null);
                }
            } else {
                return Response()->fail(100, '已恢复或未被删除', null);
            }
        } else {
            return Response()->fail(100, '权限不够', null);
        }
    }

    //硬删除用户
    public function getHardDelete($id)
    {
        $id_now = null;
        $id_now = Auth::id();
        if ($id_now == null) {
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')->where('id', $id_now)->get()->toArray();
        $res = User::withTrashed()->find($id);
        if ($stu[0]['status'] < 0) {
            if ($res != null) {
                if ($id_now == $id) {
                    return Response()->fail(100, '不能删除自己', null);
                } else {
                    $bool = $res->forceDelete();
                    if ($bool != false) {
                        Post::where('user_id', $id)->update(['user_id' => 0]);
                        return Response()->success(200, '彻底删除成功', null);
                    } else {
                        return Response()->fail(100, '彻底删除失败', null);
                    }
                }
            } else {
                return Response()->fail(100, '此用户不存在', null);
            }
        } else {
            return Response()->fail(100, '权限不够', null);
        }
    }

    //获取被软删除的用户
    public function getSoftDelUserInfo()
    {
        $res = User::onlyTrashed()->select('id', 'user_name', 'account', 'status', 'rating')
            ->orderBy('rating', 'DESC')
            ->paginate(7);
        return Response()->success(200, '成功', $res);
    }

    //获取列表信息
    public function getListInfo($status)
    { //状态
        if ($status == 0) {
            // $admin = nu
            // $admin = User::select('id', 'user_name')
            //     ->where('status', '<', 0)
            //     ->get()
            //     ->toarray();
            return Response()->success(200, '成功', null);
        } else {
            $user = User::select('id', 'user_name')
                ->where('status', 0)
                ->get()
                ->toarray();
            return Response()->success(200, '成功', $user);
        }
    }
}
